﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Owl.Contracts;
using System.Data.Entity;
using Owl.Questionnarie.Entities;
using System.Diagnostics;

namespace Owl.Questionnarie.DAL
{
    public class QuestionDAL : ICrud<Questions, Guid>
    {
        public Questions GetById(Guid key)
        {
            Trace.WriteLine("QuestionDAL", "Iniciando GetById");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    Questions q = (from question in context.Questions
                                   where question.QuestionId == key
                                   select question).FirstOrDefault();

                    Trace.WriteLine("QuestionDAL", "Retornando objeto");
                    return q;
                }


            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionDAL", ex.Message);
                throw;
            }
        }

        public List<Questions> GetAll()
        {
            Trace.WriteLine("QuestionDAL", "Iniciando GetAll");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    List<Questions> q = (from question in context.Questions
                                         select question).ToList();

                    Trace.WriteLine("QuestionDAL", "Retornando objeto");
                    return q;
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionDAL", ex.Message);
                throw;
            }
        }

        public DiscursiveQuestions Insert(DiscursiveQuestions entity)
        {
            Trace.WriteLine("QuestionDAL", "Iniciando insert");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    entity.QuestionId = Guid.NewGuid();
                    context.AddToQuestions(entity);
                    context.SaveChanges();
                    Trace.WriteLine("QuestionDAL", "Inserção realizada");

                    Trace.WriteLine("QuestionDAL", "Retornando objeto");
                    return entity = (from question in context.Questions.OfType<DiscursiveQuestions>()
                                     where question.QuestionId == entity.QuestionId
                                     select question).FirstOrDefault();
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionDAL", ex.Message);
                throw;
            }
        }


        public ChoiceQuestions Insert(ChoiceQuestions entity)
        {
            Trace.WriteLine("QuestionDAL", "Iniciando insert");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    entity.QuestionId = Guid.NewGuid();
                    context.AddToQuestions(entity);
                    context.SaveChanges();
                    Trace.WriteLine("QuestionDAL", "Inserção realizada");

                    Trace.WriteLine("QuestionDAL", "Retornando objeto");
                    return entity = (from question in context.Questions.OfType<ChoiceQuestions>()
                                     where question.QuestionId == entity.QuestionId
                                     select question).FirstOrDefault();
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionDAL", ex.Message);
                throw;
            }
        }


        public void Update(Questions entity)
        {
            Trace.WriteLine("QuestionDAL", "Iniciando update");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    Questions q = (from question in context.Questions
                                   where question.QuestionId == entity.QuestionId
                                   select question).FirstOrDefault();

                    q.Description = entity.Description;
                    context.SaveChanges();
                    Trace.WriteLine("QuestionDAL", "Alteração realizada");
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionDAL", ex.Message);
                throw;
            }
        }

        public void Delete(Guid key)
        {
            Trace.WriteLine("QuestionDAL", "Iniciando deleção");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    Questions q = (from question in context.Questions
                                   where question.QuestionId == key
                                   select question).FirstOrDefault();

                    context.DeleteObject(q);
                    context.SaveChanges();

                    Trace.WriteLine("QuestionDAL", "Deleção realizada");
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionDAL", ex.Message);
                throw;
            }
        }

        public List<Questions> GetByString(string text)
        {
            Trace.WriteLine("QuestionDAL", "Iniciando GetByString");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    List<Questions> q = (from question in context.Questions
                                         where question.Description.Contains(text)
                                         select question).ToList();

                    Trace.WriteLine("QuestionDAL", "Retornando objeto");
                    return q;
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionDAL", ex.Message);
                throw;
            }
        }


        public Questions Insert(Questions entity)
        {
            throw new NotImplementedException();
        }
    }
}
